package com.yaorange.jk.service.impl;

import com.yaorange.jk.dao.BaseDao;
import com.yaorange.jk.entity.Export;
import com.yaorange.jk.entity.PackingList;
import com.yaorange.jk.service.PackingListService;
import com.yaorange.jk.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Transactional
@Service
public class PackingListServiceImpl implements PackingListService {
    @Autowired
    private BaseDao<PackingList,String> packingListDao;

    @Autowired
    private BaseDao<Export,String> exportDao;
    @Override
    public Pagination findPage(Pagination page) {
    return packingListDao.pageByHql("from PackingList p  where p.state=1",page.getPageNo(),page.getPageSize(),null);
    }
    @Override
    public Pagination findAllPage(Pagination page) {
        return packingListDao.pageByHql("from PackingList",page.getPageNo(),page.getPageSize());
    }


    @Override
    public List<PackingList> findAll() {
        return packingListDao.getListByHQL("from PackingList");
    }

    @Override
    public void save(PackingList model) {
        System.out.println("王维ExportIds:"+model.getExportIds());
        //throw new RuntimeException();
        //packingListDao.save(model);
        //TODO BUG
        //修改报运单状态 state=2 (已装箱态)
        String[] ids = model.getExportIds().split(", ");
        StringBuilder sb = new StringBuilder();
        for (String i : ids){
            exportDao.executeSql("update EXPORT_C SET STATE=? where EXPORT_ID = ?",2,i);
            //还需保存每个报运单中的每个购销合同编号 EXPORT_NOS 格式 报运NO（购销合同编号）集合x,y|z,h
            Export e = exportDao.getByHQL("from Export c where id = ?", i);
            sb.append(e.getCustomerContract());
            sb.append("|");
        }
        System.out.println("王维setExportNos："+sb.toString());
        model.setExportNos(sb.toString());
        packingListDao.save(model);
    }



    @Override
    public void toPacking(PackingList model) {
        String[] ids = model.getId().split(", ");
        for (String id : ids){
            //packingListDao.executeHql("update PackingList set state = 1 where id in (?)",ids);
            packingListDao.executeHql("update PackingList set state = 1 where id =?",id);
        }
    }

    @Override
    public void toCancel(PackingList model) {
        String[] ids = model.getId().split(", ");
        for (String id : ids){
            //packingListDao.executeHql("update PackingList set state = 1 where id in (?)",ids);
            packingListDao.executeHql("update PackingList set state = 0 where id =?",id);
        }
    }

    @Override
    public Pagination findSubmitExportPage(Pagination page) {
        return exportDao.pageByHql("from Export e where e.state = ?",page.getPageNo(),page.getPageSize(),1);
    }

    @Override
    public void deleteById(String id) {
    packingListDao.deleteById(PackingList.class,id);
    }

    @Override
    public void update(PackingList model) {
       // packingListDao.update(model);
        //todo 修复更新覆盖Bug (done)
        PackingList pl = packingListDao.getByHQL("from PackingList where id = ?", model.getId());
        pl.setBuyer(model.getBuyer());
        pl.setDescriptions(model.getDescriptions());
        pl.setSeller(model.getSeller());
        pl.setInvoiceDate(model.getInvoiceDate());
        pl.setInvoiceNo(model.getInvoiceNo());
        pl.setMarks(model.getMarks());
        packingListDao.saveOrUpdate(pl);

    }

    @Override
    public PackingList findById(String id) {
    return packingListDao.get(PackingList.class,id);
    }
}
